package com.googlecode.hypersphere.xsd;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Element;

public class EjbJar {

	private Map<String, EjbJarElement> childrenMap = new HashMap<String, EjbJarElement>();

	private EjbJarElement root;

	public EjbJar(Element xmlRoot) {
		root = new EjbJarElement(null, xmlRoot);
		childrenMap.put(root.getName(), root);
		indexChildren(root);
	}

	private void indexChildren(EjbJarElement root) {
		for (Iterator<EjbJarElement> iter = root.getChildren().iterator(); iter
				.hasNext();) {
			EjbJarElement newChild = iter.next();
			EjbJarElement existingChild = childrenMap.get(newChild.getName());
			if (existingChild == null) {
				childrenMap.put(newChild.getName(), newChild);
			} else {
				existingChild.merge(newChild);
			}
			indexChildren(newChild);
		}
	}

	public Iterator<EjbJarElement> childIterator() {
		return childrenMap.values().iterator();
	}

	public boolean hasChild(String name) {
		return childrenMap.keySet().contains(name);
	}

	public EjbJarElement getChild(String name) {
		return childrenMap.get(name);
	}

}
